*** This code produces Table 2 and Figures 1-4 														***
*** For Tuttnauer & Wegmann in APSR 																***
*** "Voting for Votes: Opposition Parties’ Legislative Activity and Electoral Outcomes"				***

*** Install needed packages 
ssc install blindschemes, replace /* to use plotplain scheme for graphs */
ssc install outreg2, replace /* to export regression tables to RTF format */


use TW_partyterm_data.dta, clear 

*** Model 1: no interaction
fracreg logit next_voteshare conflict_rate ideo_gap vote_share ppop gdpgrowth ///
	early_election ///
	enps_cab cabinet_size   ///
	enps federal proportional ///
	frequency full_record, vce(cluster cabinet_id)
outreg2 using table2.doc, dec(3) replace ctitle("No interaction")


* Generate Figure 1	
margins, at(conflict_rate = (0 (.1) 1) (min) _factor (median) _continuous)
marginsplot, scheme(plotplain) ///
	ytitle("Predicted vote share (t+1)") ///
	xtitle("Conflict rate") ///
	title("")  ///
	addplot(hist conflict_rate, yaxis(2) yscale(axis(2) alt) ///
	fintensity(inten30) color(%30) legend(off) ytitle("Density of conflict rate", axis(2) size(small)))

graph export fig1.tif, replace


*** Model 2: institutions
fracreg logit next_voteshare c.conflict_rate##c.ppop vote_share ideo_gap gdpgrowth ///
	early_election ///
	enps_cab cabinet_size   ///
	enps federal proportional ///
	frequency full_record, vce(cluster cabinet_id)

outreg2 using table2.doc, dec(3) append ctitle("Institutions")

* Generate Figure 2
margins, dydx(conflict_rate) at(ppop = (.4 (.1) .9))
marginsplot, scheme(plotplain)  ///
	ytitle("Effect of conflict on vote share") ///
	xtitle("Opposition powers") ///
	title("") yline(0) ///
	addplot(hist ppop,  yaxis(2) yscale(axis(2) alt) ///
	fintensity(inten30) color(%30) legend(off) ytitle("Density of opposition powers", axis(2) size(small)))
	
graph export fig2.tif, replace

* Establish range  and # of cases of PPOP for which conflict has a significant effect
margins, dydx(conflict_rate) at(ppop = (.6 (.001) .7))
sum ppop if ppop<.603
di 89/169

*** Model 3: party size

fracreg logit next_voteshare c.conflict_rate##c.vote_share ideo_gap ppop gdpgrowth ///
	early_election ///
	enps_cab cabinet_size   ///
	enps federal proportional ///
	frequency full_record, vce(cluster cabinet_id)

outreg2 using table2.doc, dec(3) append ctitle("Party size")

* Generate Figure 3
margins, dydx(conflict_rate) at(vote_share = (0 (.05) .4))
marginsplot, scheme(plotplain)  ///
	ytitle("Effect of conflict on vote share") ///
	xtitle("Previous vote share") ///
	title("") yline(0) ///
	addplot(hist vote_share, yaxis(2) yscale(axis(2) alt) ///
	fintensity(inten30) color(%30) legend(off) ytitle("Density of vote share", axis(2) size(small)))
	
graph export fig3.tif, replace

*** Model 4: ideology
fracreg logit next_voteshare c.conflict_rate##c.ideo_gap vote_share ppop gdpgrowth ///
	early_election ///
	enps_cab cabinet_size   ///
	enps federal proportional ///
	frequency full_record, vce(cluster cabinet_id)

outreg2 using table2.doc, dec(3) append ctitle("Party-gov't distance")

* Generate Figure 4
margins, dydx(conflict_rate) at(ideo_gap = (0 (.1) .7))
marginsplot, scheme(plotplain)  ///
	ytitle("Effect of conflict on vote share") ///
	xtitle("Ideological distance") ///
	title("") yline(0) ///
	addplot(hist ideo_gap, yaxis(2) yscale(axis(2) alt) ///
	fintensity(inten30) color(%30) legend(off) ytitle("Density of ideological distance", axis(2) size(small)) xlabel(0 (.1) .7))	

graph export fig4.tif, replace

* Establish range  and # of cases of ideological distance for which conflict has a significant effect
margins, dydx(conflict_rate) at(ideo_gap = (.4 (.01) .5))
sum ideo_gap if ideo_gap<.42
